package com.dungnt.vtime.event;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dungnt.vtime.R;
import com.dungnt.vtime.countdown.VTCountDownItem;
import com.dungnt.vtime.countdown.VTCountDownTimer;
import com.dungnt.vtime.utils.Constant;
import com.dungnt.vtime.utils.Utils;

public class EventListAdapter extends BaseAdapter {

	private ArrayList<EventItem> data;
	private Activity activity;
	private static LayoutInflater inflater = null;
	private Typeface mFont;
	private ImageView imgThumb, imgArrow;
	private TextView tvTimeRemain, tvTime, tvTitle;

	public EventListAdapter(Activity activity, ArrayList<EventItem> data) {
		this.activity = activity;
		this.data = data;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mFont = Utils.getFontFromAssets(activity, "ASENINE.ttf");
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (data.size() <= 0)
			return 0;
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View vi = convertView;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.event_row_item, null);
			tvTitle = (TextView) vi.findViewById(R.id.event_row_title);
			tvTime = (TextView) vi.findViewById(R.id.event_row_tvTime);
			tvTimeRemain = (TextView) vi
					.findViewById(R.id.event_row_tvTimeRemain);
			tvTime.setTypeface(mFont);
			tvTimeRemain.setTypeface(mFont);
			tvTitle.setTypeface(mFont);
			imgArrow = (ImageView) vi.findViewById(R.id.event_row_arrow);
			imgThumb = (ImageView) vi.findViewById(R.id.event_row_imgThumb);
		}

		tvTitle.setText(data.get(position).getTitle());
		tvTime.setText(Utils.updateTimer(data.get(position).getTime(), true));
		tvTimeRemain.setText(Utils.updateTimer(data.get(position).getTime(),
				true));
		imgThumb.setImageBitmap(Utils.byteArrayToBitmap(data.get(position)
				.getImg()));
		if (data.get(position).getFolder() == 1)
			vi.setBackgroundResource(R.drawable.background_event_holiday);
		else
			vi.setBackgroundResource(R.drawable.background_event_business);
		long time = data.get(position).getTime();
		VTCountDownItem c = Utils.timeToCountItem(time);
		VTCountDownTimer vtimer = new VTCountDownTimer(c,
				Constant.COUNTDOWN_INTERVAL, tvTime);
		vtimer.start();
		return vi;
	}
}
